package com.company.project.service.impl;

import cn.hutool.json.JSONUtil;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.dao.AdminUserMapper;
import com.company.project.dao.CommentMapper;
import com.company.project.dao.FoodMapper;
import com.company.project.dao.OrderCommentMapper;
import com.company.project.model.Comment;
import com.company.project.model.Food;
import com.company.project.model.OrderComment;
import com.company.project.model.User;
import com.company.project.service.CommentService;
import com.company.project.core.AbstractService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by CodeGenerator on 2020/04/21.
 */
@Service
@Transactional
public class CommentServiceImpl extends AbstractService<Comment> implements CommentService {
    @Resource
    private CommentMapper commentMapper;
    @Autowired
    private AdminUserMapper userMapper;
    @Autowired
    private OrderCommentMapper orderCommentMapper;
    @Autowired
    private FoodMapper foodMapper;

    @Override
    public Result getComment(Integer shopId) {
        Condition condition = new Condition(OrderComment.class);
        condition.createCriteria().andEqualTo("shopId", shopId);
        List<OrderComment> list = orderCommentMapper.selectByCondition(condition);
        List<OrderComment> result = new ArrayList<>();
        List<Integer> orderIds = new ArrayList<>();
        if(list != null && list.size() > 0) {
            for(OrderComment comment : list) {
                if(!orderIds.contains(comment.getOrderId())){
                    List<Food> foods = new ArrayList<>();
                    orderIds.add(comment.getOrderId());
                    for(OrderComment same : list) {
                        if(same.getOrderId().equals(comment.getOrderId())) {
                            Food query = new Food();
                            query.setShopId(comment.getShopId());
                            query.setFoodId(same.getFoodId());
                            foods.add(foodMapper.selectOne(query));
                        }
                    }
                    User user = new User();
                    user.setUserId(comment.getUserId());
                    comment.setUser(userMapper.selectOne(user));
                    comment.setFoods(foods);
                    result.add(comment);
                }
            }
        }
        return ResultGenerator.genSuccessResult(result);
    }
}
